<?php

namespace app\controller;

use app\BaseController;
use app\service\WarehouseService;
use app\service\WmsWarehouseService;
use think\exception\ValidateException;
use \think\facade\Request;

/**
 * 仓库
 */
class WmsWarehouse extends BaseController
{
    /**
     * @desc 仓库类型 业务类型
     * @return void
     */
    public function info()
    {
        $info = WarehouseService::info();
        returnSuccess(200, '获取成功', $info);
    }

    //仓库列表
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['business_type_id'] = Request::param('business_type_id/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $logParam['uid'] = $uid;
        $param['uid'] = $uid;
        $param['is_export'] = Request::param('is_export/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $logParam['ip'] = Request::ip();
        $data = WarehouseService::lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    //仓库列表
    public function wms_lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['business_type_id'] = Request::param('business_type_id/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $logParam['ip'] = Request::ip();
        $data = WarehouseService::lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    //新增wms仓库
    public function add()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        // 1 快递 2社区团购 3 经销商
        //货物类型 1工业品 2 加工食品  3 汽车零配件 4 海鲜 5 电器
        $param['goods_types'] = Request::param('goods_types/s', '');
        $param['no'] = GetRandInt(6);
        //       warehouse_type_id 仓库类型
        $param['warehouse_type_id'] = Request::param('warehouse_type_id/d', 0);
        //attr_ids 仓库属性
        $param['attr_ids'] = Request::param('attr_ids/s', '');
        $param['length'] = Request::param('length/d', 0);
        $param['width'] = Request::param('width/d', 0);
        $param['height'] = Request::param('height/d', 0);
        $param['province_id'] = Request::param('province_id/d', 0);
        $param['city_id'] = Request::param('city_id/d', 0);
        $param['district_id'] = Request::param('district_id/d', 0);
        $param['address'] = Request::param('address/s', '');
        $param['longitude'] = Request::param('longitude/s', '');
        $param['dimension'] = Request::param('dimension/s', '');
        $param['status'] = Request::param('status/d', 1);
        $param['business_id'] = Request::param('business_id/s', '');
        echo json_encode($param);
        die;
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\WmsWarehouse::class)->check([
                'name' => $param['name'],
                'goods_types' => $param['goods_types'],
                'warehouse_type_id' => $param['warehouse_type_id'],
                'attr_ids' => $param['attr_ids'],
                'length' => $param['length'],
                'width' => $param['width'],
                'height' => $param['height'],
                'province_id' => $param['province_id'],
                'city_id' => $param['city_id'],
                'district_id' => $param['district_id'],
                'longitude' => $param['longitude'],
                'dimension' => $param['dimension'],
                'address' => $param['address'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        if (LonGrep($param['longitude']) == false) {
            returnFail(21201, '经度格式不正确');
        }
        if (LatGrep($param['dimension']) == false) {
            returnFail(21202, '纬度度格式不正确');
        }
        $data = WmsWarehouseService::add($param, $logParam);
        if ($data == 10000) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //修改仓库
    public function modify()
    {
        $uid = Request::param('uid/d', 0);
        $id = Request::param('id/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        // 1 快递 2社区团购 3 经销商
        //货物类型 1工业品 2 加工食品  3 汽车零配件 4 海鲜 5 电器
        $param['goods_types'] = Request::param('goods_types/s', '');
        $param['no'] = GetRandInt(6);
        //       warehouse_type_id 仓库类型
        $param['warehouse_type_id'] = Request::param('warehouse_type_id/d', 0);
        //attr_ids 仓库属性
        $param['attr_ids'] = Request::param('attr_ids/s', '');
        $param['length'] = Request::param('length/d', 0);
        $param['width'] = Request::param('width/d', 0);
        $param['height'] = Request::param('height/d', 0);
        $param['province_id'] = Request::param('province_id/d', 0);
        $param['city_id'] = Request::param('city_id/d', 0);
        $param['district_id'] = Request::param('district_id/d', 0);
        $param['address'] = Request::param('address/s', '');
        $param['longitude'] = Request::param('longitude/s', '');
        $param['dimension'] = Request::param('dimension/s', '');
        $param['status'] = Request::param('status/d', 1);
        $param['business_id'] = Request::param('business_id/s', '');
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\WmsWarehouse::class)->check([
                'name' => $param['name'],
                'goods_types' => $param['goods_types'],
                'warehouse_type_id' => $param['warehouse_type_id'],
                'attr_ids' => $param['attr_ids'],
                'length' => $param['length'],
                'width' => $param['width'],
                'height' => $param['height'],
                'province_id' => $param['province_id'],
                'city_id' => $param['city_id'],
                'district_id' => $param['district_id'],
                'longitude' => $param['longitude'],
                'dimension' => $param['dimension'],
                'address' => $param['address'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        if (LonGrep($param['longitude']) == false) {
            returnFail(21201, '经度格式不正确');
        }
        if (LatGrep($param['dimension']) == false) {
            returnFail(21202, '纬度度格式不正确');
        }
        $data = WmsWarehouseService::edit($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '修改成功');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除仓库
    public function del()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);
        $logParam['ip'] = Request::ip();
        $data = WmsWarehouseService::del($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '删除成功', $data);
        } else {
            returnFail(21101, '删除失败');
        }
    }

    //添加仓库管理员
    public function add_admin()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $pInfo = get_partner_ids($uid);
        $param['partner_id'] = $pInfo['partner_id'];
        $param['warehouse_id'] = Request::param('warehouse_id/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['password'] = Request::param('password/s', '');
        $uInfo = get_partner_info($param['mobile']);
        if (!empty($uInfo)) {
            returnFail(21401, '此手机号已存在');
        }
        if ($pInfo['type'] == 2) {
            returnFail(21401, '权限不正确');
        }
        $logParam['login_ip'] = Request::ip();
        $data = WmsWarehouseService::add_admin($param, $logParam);
        if ($data) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //仓库管理员列表
    public function admin_lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $param['warehouse_id'] = Request::param('warehouse_id/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $logParam['ip'] = Request::ip();
        $data = WmsWarehouseService::admin_lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    //修改仓库管理员
    public function modify_admin()
    {
        $id = Request::param('id/d', 0);
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $pInfo = get_partner_ids($uid);
        $param['partner_id'] = $pInfo['partner_id'];
        $param['warehouse_id'] = Request::param('warehouse_id/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['password'] = Request::param('password/s', '');
        $uInfo = get_partner_info2($param['mobile'], $id);
        if (!empty($uInfo)) {
            returnFail(21401, '此手机号已存在');
        }
        if ($pInfo['type'] == 2) {
            returnFail(21401, '权限不正确');
        }
        $logParam['login_ip'] = Request::ip();
        $data = WmsWarehouseService::modify_admin($param, $logParam, $id);
        if ($data) {
            returnSuccess(200, '修改成功');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除仓库管理员
    public function del_admin()
    {
        $id = Request::param('id/d', 0);
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $pInfo = get_partner_ids($uid);
        $param['partner_id'] = $pInfo['partner_id'];
        $param['warehouse_id'] = Request::param('warehouse_id/d', 0);
        if ($pInfo['type'] == 2) {
            returnFail(21401, '权限不正确');
        }
        $logParam['login_ip'] = Request::ip();
        $data = WmsWarehouseService::del_admin($param, $logParam, $id);
        if ($data) {
            returnSuccess(200, '删除成功');
        } else {
            returnFail(21101, '删除失败');
        }
    }
}
